﻿/// <reference path="../ExtJs/ext-all.js" />
Ext.BLANK_IMAGE_URL = 'ext/resources/images/default/s.gif';
Ext.namespace("Suucha");
var currtWind;
Ext.Window.prototype.beforeDestroy = function() {
    Ext.Window.superclass.beforeDestroy.call(this);
    Ext.destroy(this.header, this.tbar, this.bbar, this.footer, this.body, this.bwrap, this.focusEl, this.toolTarget, this.resizer, this.dd, this.proxy, this.mask);
    
    this.header = null;
    this.tbar = null;
    this.bbar = null;
    this.footer = null;
    this.body = null;
    this.bwrap = null;
    this.focusEl = null;
    this.toolTarget = null;

    this.resizer = null;
    this.dd = null;
    this.proxy = null;
    this.mask = null;
};
Ext.Panel.prototype.beforeDestroy = function() {
    Ext.Panel.superclass.beforeDestroy.call(this);
    Ext.destroy(this.header, this.tbar, this.bbar, this.footer, this.body, this.bwrap, this.focusEl, this.toolTarget, this.resizer, this.dd, this.proxy, this.mask);

    this.header = null;
    this.tbar = null;
    this.bbar = null;
    this.footer = null;
  //  this.body = null;
    this.bwrap = null;
    this.focusEl = null;
    this.toolTarget = null;

    this.resizer = null;
    this.dd = null;
    this.proxy = null;
    this.mask = null;
};
function GetUrlParms() {
    var args = new Object();
    var query = location.search.substring(1); //获取查询串 
    var pairs = query.split("&"); //在逗号处断开 
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('='); //查找name=value 
        if (pos == -1)
            continue; //如果没有找到就跳过 
        var argname = pairs[i].substring(0, pos); //提取name 
        var value = pairs[i].substring(pos + 1); //提取value 
        args[argname] = decodeURI(value); //存为属性 
    }
    return args;
}

function showErrorMessage(title, msg, details) {

    Ext.MessageBox.show({
        title: title,
        msg: msg,
        buttons: Ext.Msg.OK,
        icon: Ext.MessageBox.ERROR,
        miniWidth: 200
    });
}

function showMessage(title, msg) {
    Ext.MessageBox.show({
        title: title,
        msg: msg,
        icon: Ext.MessageBox.INFO,
        buttons: Ext.Msg.OK
    });
}

var Cookies = {};
Cookies.set = function(name, value) {
    var argv = arguments;
    var argc = arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : '/';
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
};

Cookies.get = function(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    var j = 0;
    while (i < clen) {
        j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return Cookies.getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0)
            break;
    }
    return null;
};

Cookies.clear = function(name) {
    if (Cookies.get(name)) {
        document.cookie = name + "=" +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};

Cookies.getCookieVal = function(offset) {
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
};
function openWind(url, id, text, width, height, iconCls) {
        win = new Ext.Window({
            layout: 'fit',
            width: width,
            height: height,
            id: id,
            border: false,
            iconCls: iconCls,
            shadow: true,
            animate: true,
            collapsible: true,
            autoDestroy: true,
            closable: true,
            closeAction: 'close',            
            modal: true,
            title: text,
            autoLoad: { url: url, scripts: true }
        });
        win.show();
        return win;
}
function openWindExt(id, text, width, height, iconCls, items, buttons) {
    win = new Ext.Window({
        layout: 'fit',
        width: width,
        height: height,
        id: id,
        border: false,
        iconCls: iconCls,
        shadow: true,
        animate: true,
        collapsible: true,
        autoDestroy: true,
        closable: true,
        closeAction: 'close',
        modal: true,
        title: text,
        items: items,
        buttons: buttons
    });
    win.show();
    return win;
}

function openWindHtml(html, id, text, width, height, iconCls) {
    win = new Ext.Window({
        layout: 'fit',
        width: width,
        height: height,
        id: id,
        border: false,
        iconCls: iconCls,
        shadow: true,
        animate: true,
        collapsible: true,
        autoDestroy: true,
        closable: true,
        closeAction: 'close',
        modal: true,
        title: text,
        html:html
    });
    win.show();
    return win;
}

function ajaxCalled(sender, url,dataStr, grid, fn) {
    if (!fn) {
        fn = function validateFeed(response, options) {
            var responseArray = Ext.util.JSON.decode(response.responseText);
            if (eval(responseArray.success)) {
                Ext.MessageBox.show({
                    title: '操作提示',
                    msg: responseArray.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.MessageBox.INFO,
                    miniWidth: 200
                });
                if (grid)
                    updata(grid);
                if (sender)
                    sender.close();
            }
            else {
                Ext.MessageBox.show({
                    title: '错误提示',
                    msg: responseArray.message,
                    buttons: Ext.Msg.OK,
                    icon: Ext.MessageBox.ERROR,
                    miniWidth: 200
                });
            }
        };
    }
    Ext.Ajax.request({
    url: url,
    method: 'post',
    url: url,
    params: { data: dataStr },
        success: fn
    });
}
function updata(grid) {
    grid.getStore().removeAll();
    grid.getStore().reload();
}
function checkGridSelected(grid) {
    if (grid.getSelections() != null && grid.getSelections() != '') {
        return grid.getSelections();
    }
    else {
        Ext.MessageBox.show({
            title: '操作提示',
            msg: "请选中要操作的记录",
            buttons: Ext.Msg.OK,
            icon: Ext.MessageBox.INFO,
            miniWidth: 200,
            animEl: 'deletebtn'
        });
        return false;
    }
}

function getIdFromGrid(nodes, item) {
    var id = '';
    for (var i = 0; i < nodes.length; i++) {
        id += nodes[i].get(item) + ",";
    }
    id = id.substring(0, id.length - 1);
    return id;
}
var obj = new Object();
obj = GetUrlParms();
var ArchivesType = obj['ArchivesTypeKey'];
if (!ArchivesType)
    ArchivesType = 1;
function GUID() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function submitFormJson(form, grid, sender,fn,data,urlExt) {
//alert(form.form.getValues());
    if (form.form.isValid()) {
        //        if (form.fileUpload) {
        //            form.form.submit({
        //                url: 'SaveFile.aspx',
        //                waitTitle: '请等待',
        //                waitMsg: '正在上传照片...',
        //                success: function(form, action) {
        //                    showMessage('操作成功', action.result.message);
        //                    submitInfo(action.result.message);
        //                },
        //                failure: function(form, action) {
        //                    if (action.failureType != 'client') {
        //                        showErrorMessage('操作失败', action.result.message);
        //                    }
        //                }
        //            });
        //        } else {
        //            submitInfo();
        //        } 
        var myMask = new Ext.LoadMask(form.getEl(), {
            msg: "正在保存数据,请稍候...."
        });
        var dataStr=Ext.util.JSON.encode(form.form.getValues());
        if (data) {
//            alert(data);
            dataStr = dataStr.substring(0, dataStr.length - 1) +","+ data + "}";
        } 
        //alert(dataStr);

        myMask.show();
        var url = form.url;
        if (urlExt) {
            url += urlExt;
        }
        Ext.Ajax.request({
            method: 'post',
            url: url,
            params: { data: dataStr },
            success: onSuccess,
            failure: onFailure
        });
        function onSuccess(request, options) {
            // 服务器返回json形式的结果
            myMask.hide();
            var reText = request.responseText;
            var reTextObj = Ext.util.JSON.decode(reText);
            if (eval(reTextObj.success)) {
                showMessage('操作成功', reTextObj.message);
                if (grid)
                    updata(grid);
                if (sender)
                    sender.close();
                if (fn)
                    fn.call(this,reText);
            } else {
            showErrorMessage('操作失败', reTextObj.message, '');
            }
        }
        function onFailure(request, options) {
            myMask.hide();
            showErrorMessage('操作失败', request.responseText, '');
        }
    }
}

//function submitFormByJson(form, grid, sender, fn) {
//    if (form.form.isValid()) {
//        var myMask = new Ext.LoadMask(form.getEl(), {
//            msg: "正在保存数据,请稍候...."
//        });
//        var dataStr = Ext.util.JSON.encode(form.form.getValues());
//        alert(dataStr);

//        var dataStr = "{";
//        var data = form.form.items.items;
//        for (var i = 0; i < data.length; i++) {
//            if (data[i].hiddenName) {
//                alert(data[i].getValue());
//            } else {
//            dataStr += data[i].name + ":" + data[i].getValue();
//            }
//        }
//        alert(dataStr);
//        myMask.show();

//        Ext.Ajax.request({
//            method: 'post',
//            url: form.url,
//            params: { data: dataStr },
//            success: onSuccess,
//            failure: onFailure
//        });
//        function onSuccess(request, options) {
//            // 服务器返回json形式的结果
//            myMask.hide();
//            var reText = request.responseText;
//            var reTextObj = Ext.util.JSON.decode(reText);
//            if (eval(reTextObj.success)) {
//                showMessage('操作成功', reTextObj.message);
//                if (grid)
//                    updata(grid);
//                if (sender)
//                    sender.close();
//                if (fn)
//                    fn.call(this, reText);
//            } else {
//                showErrorMessage('操作失败', reTextObj.message, '');
//            }
//        }
//        function onFailure(request, options) {
//            myMask.hide();
//            showErrorMessage('操作失败', request.responseText, '');
//        }
//    }
//}

function CheckIsCanEdit(nodes, mark) {
    var rb = true;
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].get('F40').toString() != policemanNumber) {
            rb = false;
            break;
        }
    }
    return rb;
}
var boolData = [
    ['是', '1'],
    ['否', '0']
    ]
var boolStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
boolStore.loadData(boolData);
var sexData = [
    ['男', '1'],
    ['女', '2'],
    ['未知性别', '3'],
    ['未说明性别', '4']
    ]
var sexStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
sexStore.loadData(sexData);

var DeviceStateData = [
    ['在线', '1'],
    ['正常掉线', '2'],
    ['非正常掉线', '3'],
    ['久坐不动', '4'],
    ['越界', '5'],
    ['派遣', '6']
    ]
var DeviceStateStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
DeviceStateStore.loadData(DeviceStateData);

var stateData = [
    ['启用', '1'],
    ['禁用', '2']
]
var stateStore = new Ext.data.SimpleStore({
    fields:['name','value']
});
stateStore.loadData(stateData);

var groupTypeData = [
    ['巡警', '1'],
    ['社区民警', '2'],
    ['辅警', '3']
]
var groupTypeStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
groupTypeStore.loadData(groupTypeData);

var policeMsgCaseTypeData = [
    ['盗窃案', '1'],
    ['诈骗案', '2'],
    ['抢夺案', '3'],
    ['抢劫案', '4'],
    ['扒窃案', '5']
]
var policeMsgCaseTypeStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
policeMsgCaseTypeStore.loadData(policeMsgCaseTypeData);

var usersexData = [
    ['男', '1'],
    ['女', '2']
    ]
var usersexStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
usersexStore.loadData(usersexData);

var attendceStatusData = [
    ['正在上班', '1'],
    ['已经下班', '2']
    ]
var attendceStatusStore = new Ext.data.SimpleStore({
    fields: ['name', 'value']
});
attendceStatusStore.loadData(attendceStatusData);
function submitPanelJson1(form, sender, grid, fn, data) {
    if (form.form.isValid()) {
        var myMask = new Ext.LoadMask(sender.getEl(), {
            msg: "正在保存数据,请稍候...."
        });
        var personStates = form.form.findField('PersonStates').getValue();
        var dataStr = Ext.util.JSON.encode(form.form.getValues());
        if (data) {
            dataStr = dataStr.substring(0, dataStr.length - 1) + "," + data + "}";
        }
        var url = form.url;
        myMask.show();
        Ext.Ajax.request({
            method: 'post',
            url: url,
            params: { data: dataStr, personStates: personStates },
            success: onSuccess,
            failure: onFailure
        });
        function onFailure(request, options) {
            myMask.hide();
            showErrorMessage('操作失败', request.responseText, '');
        }
        function onSuccess(request, options) {
            // 服务器返回json形式的结果
            myMask.hide();
            var reText = request.responseText;
            var reTextObj = Ext.util.JSON.decode(reText);
            if (eval(reTextObj.success)) {
                showMessage('操作成功', '保存成功');
                if (grid)
                    updata(grid);
                if (fn)
                    fn.call(this, reText);
                if (sender)
                    sender.close();
            } else {
                showErrorMessage('操作失败', reTextObj.message, '');
            }
        }

    }
};